//
//  WSearchRecordView.swift
//  iiatn
//
//  Created by mine on 18/5/10.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SwiftKeychainWrapper

protocol SearchViewDelegate: NSObjectProtocol {
    func Searchpro(button: UIButton)
}
class WSearchRecordView: UIView {

    /// 关键词
    var words = [String]()
    var mtag:String?
    weak var delegate: SearchViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 搜索界面数据获取热搜关键词
        weak var weakSelf = self
        WNetwork.shareNetworkTool.loadHotWords{ (hot_words) in
            weakSelf!.words = hot_words
            weakSelf!.setupUI()
        }
    }
    
    func setupUI() {
        // 大家都在搜
        let topView = UIView()
        topView.frame = CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH - 64)
        let hotLabel = setupLabel(title: "大家都在搜：")
        hotLabel.frame = CGRect(x: 10, y: 20, width: 200, height: 40)
        topView.addSubview(hotLabel)
        
        let width = 87
        let loop = Int((Int(SCREENW) - 15)/width)
        
        var btnx = 0
        for btnname in words{
            let btn = setupBtn(title: btnname)
            btn.x = CGFloat(10 + Int(btnx%loop)*87)
            btn.y = CGFloat(60 + Int(btnx/loop)*40)
            btn.addTarget(self, action: #selector(dosearch), for: .touchUpInside)
            topView.addSubview(btn)
            btnx = btnx + 1
        }
        
        let history = KeychainWrapper.standard.string(forKey: "pastsearch")
        print(history ?? 0)
        if history != nil {
            let nowLabel = setupLabel(title: "历史最近搜索：")
            nowLabel.frame = CGRect(x: 10, y: 100 + Int(words.count/loop)*40, width: 200, height: 40)
            topView.addSubview(nowLabel)
            
            let y_ = Int(nowLabel.y) + 40
            var btn2x = 0
            for btn2name in (history?.components(separatedBy: "|"))!{
                if !btn2name.isEmpty && btn2x < 10 {
                    let btn = setupBtn(title: btn2name)
                    btn.x = CGFloat(10 + Int(btn2x%loop)*87)
                    btn.y = CGFloat(y_ + Int(btn2x/loop)*40)
                    btn.addTarget(self, action: #selector(dosearch), for: .touchUpInside)
                    topView.addSubview(btn)
                    btn2x = btn2x + 1
                }
            }
        }
        
        addSubview(topView)
    }
    
    func dosearch(button: UIButton){
        delegate?.Searchpro(button: button)
    }
    
    func setupLabel(title: String) -> UILabel{
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = WColor(r: 0, g: 0, b: 0, a: 0.6)
        return label
    }
    
    func setupBtn(title: String) -> UIButton{
        let btn = UIButton()
        btn.width = 80
        btn.height = 25
        btn.setTitle(title, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setTitleColor(WColor(r: 0, g: 0, b: 0, a: 0.85), for: .normal)
        btn.layer.borderWidth = 0.4
        btn.layer.borderColor = UIColor.gray.cgColor
        btn.layer.cornerRadius = btn.height * 0.1
        btn.layer.masksToBounds = true
        btn.layer.rasterizationScale = UIScreen.main.scale
        return btn
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
